package com.tledu.spring.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.spring.core.entity.AjaxResult;
import com.tledu.spring.domain.Craft;
import com.tledu.spring.domain.Requirement;
import com.tledu.spring.domain.Role;
import com.tledu.spring.service.ICraftService;
import com.tledu.spring.service.IRequirementService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/craft/requirement")
@AllArgsConstructor
public class RequirementController {
    private final IRequirementService requirementService;
    private final ICraftService craftService;
    @RequestMapping()
    public String list(Requirement requirement,Model model) {
        model.addAttribute("list", requirementService.query(requirement));
        return "craft/requirement/list";
    }
    @PostMapping("list")
    @ResponseBody
    public AjaxResult<PageInfo<Requirement>> listAjaxResult(Requirement requirement, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Requirement> list = requirementService.query(requirement);
        PageInfo<Requirement> pageInfo = new PageInfo<>(list);
        return new AjaxResult<PageInfo<Requirement>>(pageInfo);
    }
    @GetMapping("/add")
    public String add(Craft craft,Model model) {
        List<Craft> lists = craftService.selectByAll(craft);
        model.addAttribute("list",lists);
        return "craft/requirement/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult<String> add(Requirement requirement) {

        requirementService.insert(requirement);
        return new AjaxResult<>("新增成功");
    }

    @DeleteMapping("/delete/{re_id}")
    @ResponseBody
    public AjaxResult<String> delete(@PathVariable Integer re_id) {
        requirementService.delete(re_id);
        return new AjaxResult<>("删除成功");
    }

    @GetMapping("/edit")
    public String edit(Integer re_id,Model model,Craft craft) {
        List<Craft> lists = craftService.selectByAll(craft);
        model.addAttribute("list",lists);
        model.addAttribute("requirement", requirementService.selectByRe_Id(re_id));
        return "craft/requirement/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult<String> edit(Requirement requirement) {
        requirementService.update(requirement);
        return new AjaxResult<>("修改成功");
    }
}
